﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>Inheritance Tests</title>
		<link href="../Common/Styles/qunit.css" type="text/css" rel="stylesheet" />

		<script src="../Common/Scripts/JQuery/jquery-1.3.2.js" type="text/javascript"></script>
		<script src="../Common/Scripts/QUnit/qunit.js" type="text/javascript"></script>
		<script src="../Common/Scripts/Microsoft/MicrosoftAjax.js" type="text/javascript"></script>
		<script src="../Common/Scripts/Microsoft/MicrosoftAjaxTemplates.js" type="text/javascript"></script>
		<script src="../Common/Scripts/ExoWeb/exoweb.js" type="text/javascript"></script>
		<script src="../Common/Scripts/ExoWeb/exoweb.model.js" type="text/javascript"></script>
		<script src="../Common/Scripts/ExoWeb/exoweb.mapper.js" type="text/javascript"></script>
		<script src="../Common/Scripts/ExoWeb/exoweb.mock.js" type="text/javascript"></script>
		<script src="../Common/Scripts/mock-driver.js" type="text/javascript"></script>
		<script src="../Common/Scripts/QUnit/qunit.ext.js" type="text/javascript"></script>
		<script type="text/javascript">

			$extend("Product", function(type) {
				type.formats.shortDesc = ExoWeb.Model.Format.fromTemplate("<Product> {Name}");
				type.formats.longDesc = type.formats.shortDesc;
			});

			$extend("UsedCar", function(type) {
				type.formats.longDesc = ExoWeb.Model.Format.fromTemplate("<UsedCar> {Name} with {MilesDriven} miles");
			});

			var context = ExoWeb.context({
				model: {
					driver: { id: "1", from: "Driver" },
					dealer: { id: "1", from: "Dealer" },
					usedCar: {id: "200", from: "Car"}
				}
			});

			setupTest("polymorphic loading", { expect: 2 }, function() {
				ok(typeof (UsedCar) == "function", "UsedCar type should be loaded b/c instance of that type was fetched");
				ok(context.model.usedCar instanceof UsedCar, "context.model.usedCar should be of type UsedCar even though base type was fetched");
			});

			setupTest("object pool and sub classes", { expect: 5 }, function() {
				ok(UsedCar.meta.get("200"), "UsedCar(200) should be in UsedCar pool");
				ok(Car.meta.get("200"), "UsedCar(200) should be in base class pool (Car)");
				ok(Product.meta.get("200"), "UsedCar(200) should be in base class pool (Product)");

				ok((new Car("200")) instanceof UsedCar, "new Car(200) should be of type UsedCar");
				ok((new Product("200")) instanceof UsedCar, "new Product(200) should be of type UsedCar");
			});

			setupTest("instanceof operator", { expect: 4 }, function() {
				ok(context.model.driver instanceof Driver, "instance of type");
				ok(context.model.driver instanceof Person, "instance of basetype");

				ok(context.model.usedCar instanceof Product, "double hop");

				ok(!(context.model.driver instanceof Dealer), "not instance of unrelated type");
			});

			setupTest("type members", { expect: 5 }, function() {					
				ok(context.model.driver.get_Cars(), "property on final type (execute)");
				ok(context.model.driver.get_Name, "property on base type (exists)");
				ok(context.model.driver.get_Name(), "property on base type (execute)");

				ok(context.model.usedCar.get_Name, "property on base type (double hop, exists)");
				ok(context.model.usedCar.get_Name(), "property on base type (double hop, execute)");
			});

			setupTest("formats", { expect: 5 }, function() {
				ok(UsedCar.formats.shortDesc === Product.formats.shortDesc, "UsedCar inherits shortDesc from base class");
				ok(Car.formats.shortDesc === Product.formats.shortDesc, "Car inherits shortDesc from base class");

				ok(UsedCar.formats.longDesc !== Product.formats.Car, "UsedCar overrides longDesc");
				ok(Car.formats.longDesc === Product.formats.longDesc, "Car inherits longDesc from base class");

				ok(context.model.usedCar.toString("longDesc").startsWith("<UsedCar>"), "toString() should use inherited formats");
			});

			setupTest("newId", { expect: 3 }, function() {
				context.model.meta.addType("Foo");
				context.model.meta.addType("Bar", Foo.meta);

				var foo = new Foo();
				var bar = new Bar();

				ok(foo.meta.id != bar.meta.id, "New objects in inheritance heirarchy should not have the same id.");

				equals(Foo.meta.get(foo.meta.id), foo, "Should be able to retrieve instance of base type by id.");
				equals(Bar.meta.get(bar.meta.id), bar, "Should be able to retrieve instance of sub type by id.");
			});

			context.ready(function() {
				executeTest("polymorphic loading");
				executeTest("object pool and sub classes");
				executeTest("instanceof operator");
				executeTest("type members");
				executeTest("formats");
				executeTest("newId");
			});
		</script>
	</head>
	<body>
		<!-- QUnit Display -->
		<h1 id="qunit-header">Test Results:</h1>
		<h2 id="qunit-banner"></h2>
		<h2 id="qunit-userAgent"></h2>
		<ol id="qunit-tests"></ol>
	</body>
</html>
